package org.krishna.spring.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.krishna.Dto.Employee;
import org.krishna.Exception.CustomGenericException;
import org.krishna.mail.SendMail;
import org.krishna.service.ServiceHandler;
import org.krishna.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Handles requests for the Student service.
 */
@Controller
public class StudentController {
	
	private static final Logger logger = LoggerFactory.getLogger(StudentController.class);
	
	/** The data transfer handler. */
    @Autowired
    @Qualifier("serviceHandler")
    private ServiceHandler serviceHandler;
    
    @Autowired
    @Qualifier("sendMail")
    private SendMail sendMail;
	
	//Map to store employees, ideally we should use database
	Map<Integer, Employee> empData = new HashMap<Integer, Employee>();
	
	@RequestMapping(value = StudentRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getDummyEmployee() {
		logger.info("Start getDummyEmployee");
		Employee emp = new Employee();
		emp.setId(9999);
		emp.setName("Dummy");
		emp.setCreatedDate(new Date());
		empData.put(9999, emp);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.GET_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getEmployee(@PathVariable("id") int empId) {
		logger.info("Start getEmployee. ID="+empId);
		
		return empData.get(empId);
	}
	
	@RequestMapping(value = StudentRestURIConstants.GET_ALL_EMP, method = RequestMethod.GET)
	public @ResponseBody List<Employee> getAllEmployees() {
		logger.info("Start getAllEmployees.");
		List<Employee> emps = new ArrayList<Employee>();
		Set<Integer> empIdKeys = empData.keySet();
		for(Integer i : empIdKeys){
			emps.add(empData.get(i));
		}
		return emps;
	}
	
	@RequestMapping(value = StudentRestURIConstants.CREATE_EMP, method = RequestMethod.POST)
	public @ResponseBody Employee createEmployee(@RequestBody Employee emp) {
		logger.info("Start createEmployee.");
		emp.setCreatedDate(new Date());
		empData.put(emp.getId(), emp);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.DELETE_EMP, method = RequestMethod.PUT)
	public @ResponseBody Employee deleteEmployee(@PathVariable("id") int empId) {
		logger.info("Start deleteEmployee.");
		Employee emp = empData.get(empId);
		empData.remove(empId);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.REGISTER,method = RequestMethod.GET)
    public @ResponseBody Boolean registerDetails(@RequestParam(value = "username", required = true) String userName,
    					   @RequestParam(value = "password", required = true) String password,
    					   @RequestParam(value = "type", required = true) String type) throws CustomGenericException{
		logger.info("Registering ....");
		logger.info("testing params");
		logger.debug("test debug");
		if(Util.isNullOrEmpty(userName)&&Util.isNullOrEmpty(password)&&Util.isNullOrEmpty(type)){
			serviceHandler.registerUser(userName, password, type);
			
		}else{
			throw new CustomGenericException("8080", "This is custom message");
		}
        //dataTransferHandler.addStudent(firstName, lastName, eMail, phone);
		return Boolean.TRUE;        
    }
	
	@RequestMapping(value = StudentRestURIConstants.TEST,method = RequestMethod.GET)
    public @ResponseBody Boolean prepare() throws Exception{
		logger.info("Registering ....");
		logger.info("testing params");
		logger.debug("test debug");
		serviceHandler.prepareReport();
		return Boolean.TRUE;        
    }
	

	@RequestMapping(value = StudentRestURIConstants.MAIL,method = RequestMethod.GET)
    public @ResponseBody Boolean sendMail() throws Exception{
		logger.info("Registering ....");
		logger.info("testing params");
		logger.debug("test debug");
		sendMail.sendMailWithAttachment();
		
		return Boolean.TRUE;        
    }
	
}
